<script>
import base from '../form/modal';
import $ from '@/common';
export default {
	extends: base,
	data() {
		return {
			hasModel: true,
			hasContinuable: true,
		};
	},
	created() {},
	watch: {
		modelLoad(v) {
			if (v) {
				this.getDefaultValue();
			}
		},
	},
	computed: {
		fieldDefaultData() {
			var data = {};
			if (this.$owner.defaultData) {
				$.setProp(data, this.$owner.defaultData);
			}
			this.formModel.fields.forEach(field => {
				if (field.def_value !== undefined) {
					data[field.code] = field.def_value;
				}
			});
			return data;
		},
	},
	methods: {
		load() {
			if (this.$refs.form) {
				this.$refs.form.setData(null);
			}
			if (this.storageData) {
				//缓存数据处理
				this.actionRecover();
			} else {
				//常规处理
				//此处是为了避开与form中初始化读取model而设置的
				if (this.hasModel) {
					//重设model路径时将重新获取model
					if (this.modelChange) {
						this.getFormModel();
					} else {
						this.getDefaultValue();
					}
				}
			}
		},
		getDefaultValue() {
			if (this.fetchDef) return;
			this.fetchDef = true;
			//取model中的默认data或者通过接口重新取得data数据
			if (this.actionModel.param.path_data) {
				$.link(this.actionModel.param.path_data + '?enable_access_token=true', this.actionData, {
					param: {
						...this.pathParam,
					},
				})
					.then(res => {
						if (res.data) {
							var data = { ...res.data };
							if (this.actionModel.param.data_mapping) {
								var mapping = {};
								if (typeof this.actionModel.param.data_mapping === 'string') {
									try {
										mapping = JSON.parse(this.actionModel.param.data_mapping);
									} catch (ex) {
										console.warn(ex);
									}
								} else {
									mapping = this.actionModel.param.data_mapping;
								}
								for (let i in mapping) {
									data[mapping[i]] = res.data[i];
								}
							}
							this.setDefaultData(data);
						}
					})
					.catch(() => {
						this.setDefaultData();
					});
			} else if (!this.isLocalModel) {
				if (this.fristLoad && this.formModel.data) {
					this.setDefaultData(this.formModel.data);
					this.fristLoad = false;
				} else {
					if (this.isBillModel && this.bill_code) {
						//单据模板通过def_value链接取得默认数据
						$.link(this.path.bill_def + '?enable_access_token=true', null, {
							param: {
								...this.pathParam,
							},
						})
							.then(res => {
								if (res.data) {
									this.setDefaultData(res.data);
								}
							})
							.catch(() => {
								this.setDefaultData();
							});
					} else {
						//其他链接重新查询模型尝试取得链接
						if (this.formModel.data) {
							$.link(this.modelPath + '?enable_access_token=true', this.actionData, {
								param: {
									...this.pathParam,
								},
							})
								.then(res => {
									this.setDefaultData(res.data.data);
								})
								.catch(() => {
									this.setDefaultData();
								});
						} else {
							this.setDefaultData();
						}
					}
				}
			} else {
				this.setDefaultData();
			}
		},
		setDefaultData(data) {
			data = $.setObj(data || {}, this.fieldDefaultData);
			this.hasModel = true;
			var param = {};
			//设置def_value渲染参数，目前支持：parent 主表的当前数值
			if (this.$owner.$mainForm && this.$owner.$mainForm.formData) {
				param.parent = this.$owner.$mainForm.formData;
			} else if (this.$owner.mainForm && this.$owner.mainForm.formData) {
				param.parent = this.$owner.mainForm.formData;
			} else {
				param.parent = this.actionData;
			}
			//需要每次重新设置默认值，因为会存在动态值
			for (let i in data) {
				data[i] = $.composeValue(data[i], param);
				if ($.trim(data[i]) === 'now()') {
					data[i] = Date.parse(new Date());
				}
			}
			if (this.$parent.actionOption && this.$parent.actionOption.importData) {
				data = {
					...data,
					...this.$parent.actionOption.importData,
				};
			}
			this.$set(this, 'defaultData', data);
			this.$forceUpdate();
			this.fetchDef = false;
		},
		formSubmitContinuable() {
			this.$refs.form.doValidate();
			this.$nextTick(() => {
				if (!this.enableSubmit) {
					this.$Message.error('表单验证失败!');
				} else {
					this.actionSubmitContinuable();
				}
				this.isValidate = true;
			});
		},
		actionSubmitContinuable() {
			if (this.isLocalData) {
				this.$owner.addRow(this.$refs.form.getData ? this.$refs.form.getData() : this.$refs.form.formData);
				this.reloadModel();
				this.clearStorage();
			} else {
				return this.putData(false).then(() => {
					this.clearStorage();
					this.reloadModel();
				});
			}
		},
		reloadModel() {
			this.modelLoad = false;
			this.$nextTick(() => {
				this.modelLoad = true;
				this.$nextTick(() => {
					this.load();
				});
			});
		},
		actionSubmit() {
			if (this.isLocalData) {
				this.$owner.addRow(this.$refs.form.getData ? this.$refs.form.getData() : this.$refs.form.formData);
				this.clearStorage();
				this.close();
			} else {
				return this.putData().then(() => {
					this.clearStorage();
				});
			}
		},
	},
};
</script> 
